//
//  KeywordManager.m
//  Grammar
//
//  Created by Rick on 7/11/13.
//
//

#import "KeywordManager.h"
#import "Keyword.h"
#import "ITTObjectSingleton.h"

#define TYKeyword @"TYKeyword"
#define TYKeywordArchiver @"TYKeywordArchiver"

@interface KeywordManager()
- (void)restore;
@end

@implementation KeywordManager
static KeywordManager *_sharedInst = nil;

+(KeywordManager*)sharedManager
{
    @synchronized(self) {
        if (_sharedInst == nil) {
            _sharedInst = [[self alloc] init];
        }
    }
    return _sharedInst;
}

- (id)init
{
    if ((self = [super init])) {
        [self restore];
    }
    return self;
}

- (void)saveData
{
    if (_keywords) {
        NSMutableData *data = [[NSMutableData alloc] init];
        NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc]
                                     initForWritingWithMutableData:data];
        [archiver encodeObject:_keywords forKey:TYKeywordArchiver];
        [archiver finishEncoding];
        [USER_DEFAULT setObject:data forKey:[NSString stringWithFormat:@"%@",TYKeyword] ];
        [archiver release];
        [data release];
        
        [USER_DEFAULT synchronize];
    }
}

- (void)readData
{
    NSData *data = [USER_DEFAULT objectForKey:[NSString stringWithFormat:@"%@",TYKeyword]];;
    if (!data) {
        return;
    }
    NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
    NSArray *array = [unarchiver decodeObjectForKey:TYKeywordArchiver];
    [unarchiver finishDecoding];
    [_keywords addObjectsFromArray:array];
    [unarchiver release];
}

- (void)addKeyword:(Keyword *)keyword
{
    for (Keyword *aKeyword in self.keywords) {
        if ([aKeyword.keywordId isEqualToString:keyword.keywordId]) {
            return;
        }
    }
    [_keywords insertObject:keyword atIndex:0];
    
    [self saveData];
}

- (void)updateKeyword:(Keyword *)keyword
{
    NSArray *array = [NSArray arrayWithArray:self.keywords];
    for (Keyword *aKeyword in array) {
        if ([aKeyword.keywordId isEqualToString:keyword.keywordId]) {
            [_keywords removeObject:aKeyword];
            [self saveData];
        }
    }
    
    [_keywords insertObject:keyword atIndex:0];
    
    [self saveData];
}

- (void)removeKeyword:(Keyword *)keyword
{
    NSArray *array = [NSArray arrayWithArray:self.keywords];
    for (Keyword *aKeyword in array) {
        if ([aKeyword.keywordId isEqualToString:keyword.keywordId]) {
            [_keywords removeObject:aKeyword];
            [self saveData];
        }
    }
}

- (NSMutableArray*)keywords
{
    if ([_keywords count] == 0) {
        [self readData];
    }
    return _keywords;
}

- (void)restore
{
    _keywords = [[NSMutableArray alloc] init];
    [self readData];
}
- (void)dealloc
{
    [_keywords release];
    [super dealloc];
}
- (void)clearMemoryData
{
    [_keywords removeAllObjects];
}

- (void)removeAllKeywords
{
    [_keywords removeAllObjects];
    [USER_DEFAULT removeObjectForKey:[NSString stringWithFormat:@"%@",TYKeyword]];
    [USER_DEFAULT synchronize];
}
@end
